import {
  GoodsDetail,
  GoodsPageResult,
  GoodsQueryParam, SkuItem,
} from '@/types/api/pms/goods';
import request from '@/utils/request';
import { AxiosPromise } from 'axios';
// @ts-ignore
import {MALL_PATH_WEB_PREFIX} from '@/types/store/prefix.d.ts';

/**
 * 获取商品分页列表
 *
 * @param queryParams
 */
export function listPageGoods(
  queryParams: GoodsQueryParam
): AxiosPromise<GoodsPageResult> {
  return request({
    url: MALL_PATH_WEB_PREFIX + '/spu/page',
    method: 'get',
    params: queryParams,
  });
}

/**
 * 获取商品详情
 *
 * @param id
 */
export function getGoodsDetail(id: string): AxiosPromise<GoodsDetail> {
  return request({
    url: MALL_PATH_WEB_PREFIX + '/spu/' + id + '/info',
    method: 'get',
  });
}

/**
 * 添加商品
 *
 * @param data
 */
export function addGoods(data: object) {
  return request({
    url: MALL_PATH_WEB_PREFIX + '/spu',
    method: 'post',
    data: data,
  });
}

/**
 * 修改商品
 *
 * @param id
 * @param data
 */
export function updateGoods(id: number, data: object) {
  return request({
    url: MALL_PATH_WEB_PREFIX + '/spu/' + id,
    method: 'put',
    data: data,
  });
}

/**
 * 修改商品SKU
 *
 * @param id
 * @param data
 */
export function updateSpuState(id: number, data: object) {
  return request({
    url: MALL_PATH_WEB_PREFIX + '/spu/' + id + '/state',
    method: 'patch',
    data: data,
  });
}

/**
 * 删除商品
 *
 * @param ids
 */
export function deleteGoods(ids: string) {
  return request({
    url: MALL_PATH_WEB_PREFIX + '/spu/list',
    method: 'delete',
    params: {ids: ids},
  });
}

/**
 * 获取SKU详情
 *
 * @param id
 */
export function getSkuFormDetail(id: string): AxiosPromise<SkuItem> {
  return request({
    url: MALL_PATH_WEB_PREFIX + '/sku/' + id + '/info',
    method: 'get',
  });
}

/**
 * 修改商品SKU
 *
 * @param id
 * @param data
 */
export function updateSku(id: number, data: object) {
  return request({
    url: MALL_PATH_WEB_PREFIX + '/sku/' + id,
    method: 'put',
    data: data,
  });
}

/**
 * 删除商品
 *
 * @param ids
 */
export function deleteSku(ids: string) {
  return request({
    url: MALL_PATH_WEB_PREFIX + '/sku/list',
    method: 'delete',
    params: {ids: ids},
  });
}
